package com.mcxx.modules.miniSalvation.order.service.write;

import com.mcxx.modules.miniSalvation.order.dao.read.OrderInspectionReadDao;
import com.mcxx.modules.miniSalvation.order.dao.write.OrderInspectionWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.InspectionOrderDTO;
import com.mcxx.modules.miniSalvation.order.entity.InspectionBatchEntity;
import com.mcxx.modules.miniSalvation.order.entity.OrderInspectionEntity;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderInspectionWriteService {

    @Autowired
    private OrderInspectionWriteDao orderInspectionWriteDao;
    @Autowired
    private OrderInspectionReadDao orderInspectionReadDao;


    public int insert(OrderInspectionEntity entity) {
    	return orderInspectionWriteDao.insert(entity);
    }


    public int insertIgnoreNull(OrderInspectionEntity entity) {
    	return orderInspectionWriteDao.insertIgnoreNull(entity);
    }


    public int update(OrderInspectionEntity entity) {
    	return orderInspectionWriteDao.update(entity);
    }


    public int updateIgnoreNull(OrderInspectionEntity entity) {
    	return orderInspectionWriteDao.updateIgnoreNull(entity);
    }


    public int delete(OrderInspectionEntity entity) {
    	return orderInspectionWriteDao.delete(entity);
    }


    public void deleteByBatchId(String batchId) {
        orderInspectionWriteDao.deleteByBatchId(batchId);
    }


    public void generateAuditData(InspectionBatchEntity batchEntity) {
        OrderInspectionEntity entity = orderInspectionReadDao.getByBatchOrderId(batchEntity.getId());
        if (null == entity) {
            OrderInspectionEntity inspectionEntity = new OrderInspectionEntity();
            inspectionEntity.setBatchOrderId(batchEntity.getId());
            inspectionEntity.setCreateTime(new Date());
            inspectionEntity.setInspectionState(Constant.BatchPushState.NO);
            inspectionEntity.setUpdateTime(new Date());
            orderInspectionWriteDao.insertIgnoreNull(inspectionEntity);
        }
    }


    public void saveDataAudit(InspectionOrderDTO batchDTO, UserAccountVo curUserAccount) {
        OrderInspectionEntity entity = new OrderInspectionEntity();
        BeanUtils.copyProperties(batchDTO, entity);
        entity.setInspectionState(Constant.BatchPushState.YES);
        entity.setInspectionPeople(curUserAccount.getName());
        entity.setUpdateTime(new Date());
        entity.setBatchOrderId(batchDTO.getBatchId());
        entity.setInspectionPeoplePhone(batchDTO.getInspectionPhone());
        Date date = DateUtil.str2Date(batchDTO.getInspectionTime(), DateUtil.sdf);
        entity.setInspectionTime(date);
        orderInspectionWriteDao.updateIgnoreNull(entity);
    }
}
